Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #### By: TheMightyFozz ####
- bl_info = {
- "name": "OpenFileLocation",
- "description": "Opens saved .blend file in default ",
- "author": "TheMightyFozz",
- "version": (0, 2),
- "blender": (3, 3, 1),
- "location": "View_3D > Open File Browser",
- "warning": "", # used for warning icon and text in addons panel
- "doc_url": "http://wiki.blender.org",
- "tracker_url": "https://developer.blender.org",
- "support": "COMMUNITY",
- "category": "Import-Export",
- }
- #Import dependencies
- import os, bpy, platform, subprocess
- #Set variables
- ###########The Thing I want the button to do ###############
- class OpenFL_OT_DirectoryOpen(bpy.types.Operator):
- bl_idname = "cross.directory_open"
- bl_label = "Open Folder" # label needs to be generic between os flavours
- bl_options = {'REGISTER'}
- directory: bpy.props.StringProperty()
- def execute(self, context):
- if self.directory:
- sysInfo = platform.system()
- print(sysInfo)
- if sysInfo == 'darwin':
- # If Mac Then use Open
- subprocess.check_call(['open', '--', self.directory])
- elif sysInfo == 'Linux':
- # If Linux use xdg-open
- subprocess.Popen(["xdg-open", self.directory])
- elif sysInfo == 'Windows':
- # If Windows use explorer
- os.startfile(self.directory)
- return {'FINISHED'}
- else:
- self.report({"ERROR"}, "Missing 'directory' variable")
- ############################################################
- #Creates UI Panel
- class OpenFL_PT_main_panel(bpy.types.Panel):
- """Opens Current File Location In File Browser"""
- bl_label = "Open File Location"
- bl_idname = "ofl_pt.main_panel"
- bl_space_type = "VIEW_3D"
- bl_region_type = 'UI'
- bl_category = 'Open File Location'
- def draw(self, context):
- layout = self.layout
- row = layout.row()
- row.label(text= "Open .blend Location", icon= 'FILEBROWSER')
- row = layout.row()
- cross = row.operator("cross.directory_open")
- cross.directory = os.path.dirname(bpy.data.filepath)
- #Register and Unregister
- def register():
- bpy.utils.register_class(OpenFL_PT_main_panel)
- bpy.utils.register_class(OpenFL_OT_DirectoryOpen)
- def unregister():
- bpy.utils.unregister_class(OpenFL_PT_main_panel)
- bpy.utils.unregister_class(OpenFL_OT_DirectoryOpen)
- if __name__ == "__main__":
- register()
- ################Notes from V 0.1 ###################
- #maybe issues with windows
- #this code may help
- #open_wrapper(os.path.dirname(bpy.data.filepath))
- #Could streamline code
- #(if you want to make your operator more modular you can also set it as a prop and use that in draw:
- #row.operator("findandopen.run",text ="Open Folder").directory = os.path.dirname(bpy.data.filepath)
- ################Notes For V 0.3 ###################
- # @melmass
- # Technically though it's not ideal that the os.path operation happens in draw,
- # but it was to give you the context of how to add prop to operators, it would be more efficient
- # in this case to keep it in execute so it's only run when you ask it and not every time it's on screen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement